package com.origin.app.page.company_list

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.google.gson.GsonBuilder
import com.origin.app.MyApplication
import com.origin.app.bean.BaseNetBean
import com.origin.app.bean.CompanyBean
import com.origin.app.bean.UserInfoBean
import com.origin.app.page.company_list.adapter.CompanyAdapter
import com.origin.app.utils.AppUtils
import com.origin.app.utils.NetAppUtils
import com.origin.app.utils.UserInfoUtils
import com.origin.baselibrary.viewmodel.BaseViewModel
import com.origin.utils.MD5Utils
import com.origin.utils.ToastUtils
import com.origin.utils.log.LogUtils
import ikidou.reflect.TypeBuilder

class CompanyListViewModel : BaseViewModel() {
    val refreshListMutableLiveData = MutableLiveData<Long>()
    fun getCompanyListUrl() {
        viewModelSuspend {
            val type = TypeBuilder.newInstance(BaseNetBean::class.java)
                .beginSubType(List::class.java)
                .addTypeParam(CompanyBean::class.java)
                .endSubType()
                .build()
            var netBean = NetAppUtils.get<BaseNetBean<List<CompanyBean>>>(
                NetAppUtils.getCompanyListUrl + "?company_code=${UserInfoUtils.getUserInfo().company_code}",
                type,
                this
            );
            if (netBean.isOk) {
                LogUtils.__e("长度 " + netBean.data.size)
                if (netBean.data.size == 0) {
                    return@viewModelSuspend;
                }
                splitMap(netBean.data)
            }
        }
    }


    var map = mutableMapOf<Int, MutableList<CompanyBean>>()

    var companyBeanList = mutableListOf<CompanyBean>();
    private fun splitMap(list: List<CompanyBean>) {
        for (companyBean in list) {
            if (map.containsKey(companyBean.level)) {
                map[companyBean.level]?.add(companyBean)
            } else {
                map[companyBean.level] = mutableListOf(companyBean)
            }
        }
        // 重新排序
        if (!map.containsKey(1)) {
            AppUtils.showLayoutToast("没有一级");
            return;
        }
        companyBeanList.clear();
        map[1]?.forEach {
            it.tag+="${it.parent_id},"
            companyBeanList.add(it)
            it.nextSize = (findNextLevel(it,it.parent_id.toString()));
        }


        refreshListMutableLiveData.postValue(System.currentTimeMillis())
    }

    fun findNextLevel(companyBean: CompanyBean,parentId:String) :Int{
        var size=0;
        var nextLevel = companyBean.level + 1;
        if (!map.containsKey(nextLevel)) {
            return size;
        }
        var lastSize=0
        map[nextLevel]?.forEach {
            if (it.parent_id == companyBean.id) {
                size++;
                it.tag= parentId+","+it.parent_id
                companyBeanList.add(it)
                it.nextSize =   findNextLevel(it,it.tag)
                lastSize+=it.nextSize;
            }
        }
        return size +lastSize;
    }
}